/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.perpustakaan;

import com.ivanbiz.account.AccountService;
import com.ivanbiz.buku.Buku;
import com.ivanbiz.buku.BukuService;
import com.ivanbiz.injection.CallCtx;
import com.ivanbiz.properties.SISBeanConstants;
import com.ivanbiz.properties.SISConstants;
import com.ivanbiz.properties.SISScreenConstants;
import com.ivanbiz.siswa.SiswaService;
import com.ivanbiz.tools.IvanBiz;
import com.ivanbiz.user.User;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;

/**
 *
 * @author Ivan
 */
public class PerpustakaanAction extends IvanBiz {

    ActionMessages actionMessages = new ActionMessages();
    DateFormat formatter = new SimpleDateFormat(SISConstants.DATE_FORMAT);

    public ActionForward perpustakaanList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        session.removeAttribute(SISConstants.LIST_PEMINJAMAN_BUKU);
        return mapping.findForward(SISScreenConstants.PERPUSTAKAAN_LIST);
    }

    public ActionForward peminjamanBuku(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        return mapping.findForward(SISScreenConstants.PEMINJAMAN_BUKU);
    }

    public ActionForward bukuAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        PerpustakaanForm perpustakaanForm = (PerpustakaanForm) form;
        if (session.getAttribute(SISConstants.LIST_PEMINJAMAN_BUKU) == null) {
            List listPeminjamanBuku = new ArrayList();
            session.setAttribute(SISConstants.LIST_PEMINJAMAN_BUKU, listPeminjamanBuku);
        }
        return mapping.findForward(SISScreenConstants.BUKU_ADD);
    }

    public ActionForward bukuSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PeminjamanBukuService peminjamanBukuService = (PeminjamanBukuService) CallCtx.getContext(request).getBean(SISBeanConstants.PEMINJAMAN_BUKU_SERVICE);
        BukuService bukuService = (BukuService) CallCtx.getContext(request).getBean(SISBeanConstants.BUKU_SERVICE);
        PerpustakaanForm perpustakaanForm = (PerpustakaanForm) form;
        HttpSession session = request.getSession(true);
        User user = (User) session.getAttribute(SISConstants.USER);
        try {
            boolean isValid = bukuService.cekIsEmptyKode(request.getParameter(SISConstants.BUKU));
            if (!isValid) {
                actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.CODE_IS_EMPTY));
                saveMessages(request, actionMessages);
                return mapping.findForward(SISScreenConstants.BUKU_ADD);
            } else {
                boolean bukuIsValid = bukuService.cekFlagBuku(perpustakaanForm.getBuku(), SISConstants.NOL);
                if (!bukuIsValid) {
                    actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.BOOK_IS_BORROW));
                    saveMessages(request, actionMessages);
                    return mapping.findForward(SISScreenConstants.BUKU_ADD);
                } else {
                    List listPeminjamanBuku = (List) session.getAttribute(SISConstants.LIST_PEMINJAMAN_BUKU);
                    boolean cek = true;
                    for (int x = 0; x < listPeminjamanBuku.size(); x++) {
                        PeminjamanBuku peminjamanBuku = (PeminjamanBuku) listPeminjamanBuku.get(x);
                        if (perpustakaanForm.getBuku().equals(peminjamanBuku.getBuku().getKodeBuku())) {
                            cek = false;
                            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.BOOK_IS_EXIST));
                            saveMessages(request, actionMessages);
                            return mapping.findForward(SISScreenConstants.BUKU_ADD);
                        }
                    }
                    if (cek == true) {
                        PeminjamanBuku peminjamanBuku = new PeminjamanBuku();
                        Buku buku = bukuService.loadByKode(perpustakaanForm.getBuku());
                        peminjamanBuku.setBuku(buku);
                        peminjamanBuku.setCreatedBy(user.getNama());
                        peminjamanBuku.setCreatedDt(new Date());
                        Date date = new Date();
                        date.setHours(0);
                        date.setMinutes(0);
                        date.setSeconds(0);
                        peminjamanBuku.setTanggalPinjam(date);
                        String tanggalKembali = perpustakaanForm.getTanggalKembali();
                        peminjamanBuku.setTanggalKembali(formatter.parse(tanggalKembali));
                        listPeminjamanBuku.add(peminjamanBuku);
                        session.setAttribute(SISConstants.LIST_PEMINJAMAN_BUKU, listPeminjamanBuku);
                        perpustakaanForm.reset(mapping, request);
                    }
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(PerpustakaanAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.BUKU_ADD);
    }

    public ActionForward bukuAddDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        if (session.getAttribute(SISConstants.LIST_PEMINJAMAN_BUKU) != null) {
            List listPeminjamanBuku = (List) session.getAttribute(SISConstants.LIST_PEMINJAMAN_BUKU);
            for (int x = 0; x < listPeminjamanBuku.size(); x++) {
                PeminjamanBuku peminjamanBuku = (PeminjamanBuku) listPeminjamanBuku.get(x);
                if (peminjamanBuku.getBuku().getKodeBuku().equals(request.getParameter(SISConstants.BUKU))) {
                    listPeminjamanBuku.remove(x);
                    break;
                }
            }
            session.setAttribute(SISConstants.LIST_PEMINJAMAN_BUKU, listPeminjamanBuku);
        }
        return mapping.findForward(SISScreenConstants.BUKU_ADD);
    }

    public ActionForward cekKartuPerpus(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        ActionMessages message = new ActionMessages();
        MessageResources messageResources = getResources(request);
        SiswaService siswaService = (SiswaService) CallCtx.getContext(request).getBean(SISBeanConstants.SISWA_SERVICE);
        AccountService accountService = (AccountService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_SERVICE);
        try {
            boolean accuntCardIsValid = accountService.cekIsEmptyKode(request.getParameter(SISConstants.KARTU));
            boolean siswaCardIsValid = siswaService.cekIsEmptyKode(request.getParameter(SISConstants.KARTU));
            if (!accuntCardIsValid && !siswaCardIsValid) {
                response.getWriter().println("<font color=red>" + messageResources.getMessage(SISConstants.CARD_NOT_FOUND) + "</font>");
            } else {
                response.getWriter().println("<font color=green>" + messageResources.getMessage(SISConstants.CARD_IS_EXIST) + "</font>");
            }
        } catch (Exception e) {
            message.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, message);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(null);
    }

    public ActionForward cekBuku(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        ActionMessages message = new ActionMessages();
        BukuService bukuService = (BukuService) CallCtx.getContext(request).getBean(SISBeanConstants.BUKU_SERVICE);
        MessageResources messageResources = getResources(request);
        try {
            boolean isValid = bukuService.cekIsEmptyKode(request.getParameter(SISConstants.BUKU));
            if (!isValid) {
                response.getWriter().println("<font color=red>" + messageResources.getMessage(SISConstants.CODE_IS_EMPTY) + "</font>");
            } else {
                response.getWriter().println("<font color=green>" + messageResources.getMessage(SISConstants.CODE_IS_EXIST) + "</font>");
            }

//            boolean isFree = bukuService.cekFlagBuku(request.getParameter("buku"), "0");
//            if (!isFree) {
//                response.getWriter().println("<font color=red>" + messageResources.getMessage("error.buku.dipinjam") + "</font>");
//            } else {
//                response.getWriter().println("<font color=green>" + messageResources.getMessage("error.buku.tidakDipinjam") + "</font>");
//            }
        } catch (Exception e) {
            message.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, message);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(null);
    }

    public ActionForward peminjamanBukuSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PeminjamanBukuService peminjamanBukuService = (PeminjamanBukuService) CallCtx.getContext(request).getBean(SISBeanConstants.PEMINJAMAN_BUKU_SERVICE);
        SiswaService siswaService = (SiswaService) CallCtx.getContext(request).getBean(SISBeanConstants.SISWA_SERVICE);
        AccountService accountService = (AccountService) CallCtx.getContext(request).getBean(SISBeanConstants.ACCOUNT_SERVICE);
        PerpustakaanForm perpustakaanForm = (PerpustakaanForm) form;
        HttpSession session = request.getSession(true);
        try {
//            boolean accuntCardIsValid = accountService.cekIsEmptyKode(request.getParameter("kartu"));
//            boolean siswaCardIsValid = siswaService.cekIsEmptyKode(request.getParameter("kartu"));
//            if (!accuntCardIsValid && !siswaCardIsValid) {
//                actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.kartu.tidakAda"));
//                saveMessages(request, actionMessages);
//                return mapping.findForward("peminjamanBuku");
//            } else {
            peminjamanBukuService.savePeminjamanBuku((List) session.getAttribute(SISConstants.LIST_PEMINJAMAN_BUKU), perpustakaanForm.getKartuPerpus());
            perpustakaanForm.reset(mapping, request);
//            }
        } catch (Exception ex) {
            Logger.getLogger(PerpustakaanAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.PERPUSTAKAAN_LIST);
    }

    public ActionForward cariBuku(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PeminjamanBukuService peminjamanBukuService = (PeminjamanBukuService) CallCtx.getContext(request).getBean(SISBeanConstants.PEMINJAMAN_BUKU_SERVICE);
        try {
            List listCariBuku = peminjamanBukuService.cekBukuKeluar();
            request.setAttribute(SISConstants.LIST_CARI_BUKU, listCariBuku);
        } catch (Exception ex) {
            Logger.getLogger(PerpustakaanAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.CARI_BUKU);
    }

    public ActionForward cariBukuKeluar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PeminjamanBukuService peminjamanBukuService = (PeminjamanBukuService) CallCtx.getContext(request).getBean(SISBeanConstants.PEMINJAMAN_BUKU_SERVICE);
        PerpustakaanForm perpustakaanForm = (PerpustakaanForm) form;
        try {
            List listCariBuku = peminjamanBukuService.cariBukuKeluar(perpustakaanForm.getBuku(), SISConstants.NO);
            request.setAttribute(SISConstants.LIST_CARI_BUKU, listCariBuku);
        } catch (Exception ex) {
            Logger.getLogger(PerpustakaanAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.CARI_BUKU);
    }

    public ActionForward cariKeterlambatanBuku(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PeminjamanBukuService peminjamanBukuService = (PeminjamanBukuService) CallCtx.getContext(request).getBean(SISBeanConstants.PEMINJAMAN_BUKU_SERVICE);
        try {
            List listKeterlambatanBuku = peminjamanBukuService.cariKeterlambatan(new Date());
            request.setAttribute(SISConstants.LIST_KETERLAMBATAN_BUKU, listKeterlambatanBuku);
        } catch (Exception ex) {
            Logger.getLogger(PerpustakaanAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.CARI_KETERLAMBATAN_BUKU);
    }

    public ActionForward cariKartuPerpus(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PeminjamanBukuService peminjamanBukuService = (PeminjamanBukuService) CallCtx.getContext(request).getBean(SISBeanConstants.PEMINJAMAN_BUKU_SERVICE);
        PerpustakaanForm perpustakaanForm = (PerpustakaanForm) form;
        HttpSession session = request.getSession(true);
        try {
            List listPengembalianBuku = peminjamanBukuService.loadByKodeAccount(perpustakaanForm.getKartuPerpus());
            request.setAttribute(SISConstants.LIST_PENGEMBALIAN_BUKU, listPengembalianBuku);
        } catch (Exception ex) {
            Logger.getLogger(PerpustakaanAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.PENGEMBALIAN_BUKU);
    }

    public ActionForward pengembalianBuku(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        return mapping.findForward(SISScreenConstants.PENGEMBALIAN_BUKU);
    }

    public ActionForward bukuKembali(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        PeminjamanBukuService peminjamanBukuService = (PeminjamanBukuService) CallCtx.getContext(request).getBean(SISBeanConstants.PEMINJAMAN_BUKU_SERVICE);
        try {
            peminjamanBukuService.pengembalianBuku(Long.parseLong(request.getParameter(SISConstants.ID)));
            List listPengembalianBuku = peminjamanBukuService.loadByKodeAccount(request.getParameter(SISConstants.PERPUS));
            request.setAttribute(SISConstants.LIST_PENGEMBALIAN_BUKU, listPengembalianBuku);
        } catch (Exception ex) {
            Logger.getLogger(PerpustakaanAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.PENGEMBALIAN_BUKU);
    }
}
